package com.sys.servlet.mgr.trainer;

import com.sys.entity.User;
import com.sys.pojo.JsonResult;
import com.sys.service.TrainerService;
import com.sys.utils.WriteJson;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;

// 显示所有名片
/*
 1. 调用service查询所有名片（List<Card>）
 2. 将所有名片 (List<Card>) 传给jsp
        request.setAttribute()
        request.getRequestDispatcher().forword() 转发

 */
@WebServlet(urlPatterns = "/mgr/trainer/list")
public class ListTrainerServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        PrintWriter writer = resp.getWriter();

        // 如何获取登录人的信息，主要是登录人的uid
        HttpSession session = req.getSession();
        User user = (User)session.getAttribute("user");
        Integer uid = user.getUid();

        String tid = req.getParameter("tid");
        String tname = req.getParameter("tname");
        TrainerService trainerservice = new TrainerService();
        JsonResult jr = new JsonResult();
        try {
            List<Map<String, Object>> list = trainerservice.showList(uid,tid,tname);
           jr.setCode(0);
           jr.setData(list);
        } catch (Exception e) {
            e.printStackTrace();
            jr.setCode(500);
            jr.setMsg("查询发生错误");

        }
        WriteJson.write(resp,jr);
    }
}
